package com.maotouying.owlsmart.SmartUtil.Http;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotouying.owlsmart.R;
import com.maotouying.owlsmart.SmartUtil.Sqlite.DatabaseHelper;

import java.util.ArrayList;

public class localSqlite {
    /*
    *
    * 这个类折磨的我挺久的，当工具用就行
    *
    *
     */

    private DatabaseHelper helper;
    private SQLiteDatabase db;
    private Context mContext;
    //拿当前用户表里面的token
    public String getToken(Context context){
        this.mContext = context;
        initsqlite(mContext);
//先读取数据
        Log.d("--------1------",  JSONArray.toJSONString(getResults("userTable")));
//读取之后拆分
        String str1= JSONArray.toJSONString(getResults("userTable")).replace("[","");
        String str2 = str1.replace("]","");


        JSONObject jsonobj = JSON.parseObject(str2);
        //String username = jsonobj.getString("username");
        //拿到token
        String token = jsonobj.getString("token");
        return token;

    }


    public String getUserinfo(Context context) {
        this.mContext = context;
        initsqlite(mContext);
        //先读取数据，读取之后拆分
        String str1 = JSONArray.toJSONString(getLimit1("userTable"));

        try {
            JSONObject jsonobj = JSON.parseObject(str1.replace("[","").replace("]",""));

            //拿到token
            String username = jsonobj.getString("email");//读取邮箱
            String password = jsonobj.getString("password");
            return username+"#"+password;
        }catch(Exception e){
            System.out.println("getUserinfo:::"+e.toString());//出现异常的处理
            return "#";
        }
    }

    public String getUserArray(Context context) {


        this.mContext = context;

        initsqlite(mContext);

//先读取数据读取之后拆分
        String str1 = JSONArray.toJSONString(getResults("userTable"));

        System.out.println("json:::" + str1);

        //返回的是一个json数组

        return str1;


    }


    public String getPresent(Context context) {


        this.mContext = context;

        initsqlite(mContext);

//先读取数据读取之后拆分
        String str1 = JSONArray.toJSONString(getResults("presentTable"));

        System.out.println("json:::" + str1);

        //返回的是一个json数组

        return str1;


    }


    public boolean isUserData(String email) {

        boolean state = false;
        //历史初始化
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select email from userTable where email = "+email, null);
        while (cursor.moveToNext()) {
            //遍历出表名
            String title_str = cursor.getString(0);
            state = title_str.equals(email);
            // Log.i("System.out", title_str);
        }

        return state;
    }



//    public boolean isUserData() {
//
//        boolean state = false;
//        Cursor c = db.rawQuery("select * from userTable", null);
//        if (c.getCount() > 0) {
//            state=true;
//        }
//        else {
//            state=false;
//        }
//        return state;
//    }

    public int UserDatamount() {
        int amount;
        Cursor c = db.rawQuery("select * from userTable", null);
        if (c.getCount() > 0) {
            //返回1说明有数据
            amount=1;
        } else{
            amount=c.getCount();
        }
        System.out.println("amount:::"+ amount);
        return amount;
    }

    public void initsqlite(Context context) {

        helper = new DatabaseHelper(context, "db_owlsmart", null, R.string.db_version);//dbName数据库名
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
    }
    //这个函数是读取数据库并转换为json字符串的
    private JSONArray getResults(String userTable)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        String searchQuery = " SELECT  email,password,userProfileImage FROM "+ userTable ;
        Cursor cursor = db.rawQuery(searchQuery, null );
        JSONArray resultSet = new JSONArray();
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {

            int totalColumn = cursor.getColumnCount();
            JSONObject rowObject = new JSONObject();

            for( int i=0 ;  i< totalColumn ; i++ )
            {
                if( cursor.getColumnName(i) != null )
                {
                    try
                    {
                        if( cursor.getString(i) != null )
                        {
                            Log.d("userTable", cursor.getString(i) );
                            rowObject.put(cursor.getColumnName(i) ,  cursor.getString(i) );
                        }
                        else
                        {
                            rowObject.put( cursor.getColumnName(i) ,  "" );
                        }
                    }
                    catch( Exception e )
                    {
                        Log.d("userTable", e.getMessage()  );
                    }
                }
            }
            resultSet.add(rowObject);
            cursor.moveToNext();
        }
        cursor.close();
        return resultSet;
    }





    //这个函数是第一条数据库并转换为json字符串的
    private JSONArray getLimit1(String Table)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        String searchQuery = "SELECT  * FROM "+Table+ " where id =1 limit 0,1 " ;
        Cursor cursor = db.rawQuery(searchQuery, null );
        JSONArray resultSet = new JSONArray();
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {

            int totalColumn = cursor.getColumnCount();
            JSONObject rowObject = new JSONObject();

            for( int i=0 ;  i< totalColumn ; i++ )
            {
                if( cursor.getColumnName(i) != null )
                {
                    try
                    {
                        if( cursor.getString(i) != null )
                        {
                            Log.d("userTable", cursor.getString(i) );
                            rowObject.put(cursor.getColumnName(i) ,  cursor.getString(i) );
                        }
                        else
                        {
                            rowObject.put( cursor.getColumnName(i) ,  "" );
                        }
                    }
                    catch( Exception e )
                    {
                        Log.d("userTable", e.getMessage()  );
                    }
                }
            }
            resultSet.add(rowObject);
            cursor.moveToNext();
        }
        cursor.close();
        return resultSet;
    }


}
